package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.VoucherBusinessBind;
import com.turing.post.organization.util.VoucherBusinessBindDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author wzd
 * @2013-03-12
 */
@Stateless(mappedName = "organization.impl.VoucherBusinessBindDaoImpl")
@Remote
@Local
public class VoucherBusinessBindDaoImpl implements VoucherBusinessBindDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 根据业务类型检索业务凭证种类对照列表---注：模糊查询
	 * 
	 * @param businessType
	 *            业务类型
	 * @param page
	 *            页码
	 * @return 对照列表
	 */
	@SuppressWarnings("unchecked")
	public List<VoucherBusinessBind> getVoucherBusinessBindByBusinessType(
			PCPSWebInput webInput, String businessType, Integer page) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String issueCompany = PCPSUtil.getIssueCompanyNo(webInput);
		if (page == -1) {
			String sql = "select t.* from voucherbusinessbind t,voucher s where t.voucherid=s.voucherid and s.issuecompanyno='"
					+ issueCompany+"'";
			if (!PCPSUtil.isEmpty(businessType)) {
				sql = sql + " and t.businessType = '" + businessType + "'";
			}
			sql = sql + " order by t.VoucherId,t.businessType";
			Query query = _em.createNativeQuery(sql, VoucherBusinessBind.class);
			return query.getResultList();
		} else {
			int maxResult = Config.PAGE_SIZE;
			int startPosition = maxResult * page;
			String sql = "select * from voucherbusinessbind t,voucher s where t.voucherid=s.voucherid and s.issuecompanyno='"
					+ issueCompany + "'";
			if (!PCPSUtil.isEmpty(businessType)) {
				sql = sql + " and t.businessType = '" + businessType + "'";
			}
			sql = sql + " order by t.VoucherId,t.businessType";
			Query query = _em.createNativeQuery(sql, VoucherBusinessBind.class)
					.setFirstResult(startPosition).setMaxResults(maxResult);
			return query.getResultList();
		}
	}

	/**
	 * 查询业务凭证种类对照记录条数
	 * 
	 * @param businessType
	 *            业务类型
	 * @return BigInteger 记录总数
	 */
	@Override
	public BigInteger getVoucherBusinessBindByBusinessTypeTotal(
			PCPSWebInput webInput, String businessType) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo()))
			return null;
		String issueCompany = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0) from voucherbusinessbind t,voucher s where t.voucherid=s.voucherid and s.issuecompanyno='"
				+ issueCompany + "'";
		if (!PCPSUtil.isEmpty(businessType)) {
			sql = sql + " and t.businessType = '" + businessType + "'";
		}
		sql = sql + " order by t.VoucherId,t.businessType";
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	/**
	 * 根据凭证id和业务类型查询业务凭证种类对照对象
	 * 
	 * @param voucherId
	 *            凭证Id
	 * @param businessType
	 *            业务类型
	 * @return 对照对象
	 */
	@Override
	public VoucherBusinessBind getVoucherBusinessBind(BigInteger voucherId,
			String businessType) {
		String sql = "select * from VoucherBusinessBind where voucherId="
				+ voucherId + " and businessType='" + businessType + "'";
		Query query = _em.createNativeQuery(sql, VoucherBusinessBind.class);
		return (VoucherBusinessBind) query.getSingleResult();
	}

	/**
	 * 新增业务凭证种类对照
	 * 
	 * @param VoucherBusinessBind
	 *            业务凭证种类对照
	 * @throws PCPSApplicationException
	 */
	public void insertVoucherBusinessBind(
			VoucherBusinessBind voucherbusinessbind)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, voucherbusinessbind);
	}

	/**
	 * @删除业务凭证种类对照
	 * @param VoucherBusinessBind
	 *            业务凭证种类对照
	 * @throws PCPSApplicationException
	 */
	public void deleteVoucherBusinessBind(
			VoucherBusinessBind voucherbusinessbind)
			throws PCPSApplicationException {
		VoucherBusinessBind vbb = getVoucherBusinessBindByvoucherId(voucherbusinessbind);
		if (vbb != null) {
			GeneralDao.remove(_em, vbb);
		}
	}

	/**
	 * 根据凭证id查询业务凭证种类对照对象
	 * 
	 * @param voucherId
	 *            凭证Id
	 * @return 对照对象
	 */
	public VoucherBusinessBind getVoucherBusinessBindByvoucherId(
			VoucherBusinessBind voucherbusinessbind) {
		return _em.find(VoucherBusinessBind.class, voucherbusinessbind);
	}
}
